package com.sly.netty.controller;

import com.sly.netty.core.Message;
import com.sly.netty.netty.NettyServerHandler;
import com.sly.netty.result.BaseResult;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * chat controller
 *
 * @author SLY
 * @date 2025/2/24
 */
@Slf4j
@RestController
@RequestMapping("/chat")
public class ChatController {

    /**
     * 消息重定向接口
     *
     * @param message 消息对象
     * @return {@link BaseResult}<{@link Object}>
     * @author SLY
     * @date 2025/2/24
     */
    @RequestMapping("/redirect")
    public BaseResult<Object> redirect(@RequestBody Message message) {
        Channel channel = NettyServerHandler.CHANNEL_MAP.get(message.getToUser());
        if (channel != null) {
            channel.writeAndFlush(message.getContent());
        } else {
            log.info("目标用户没有在线，放入队列或者丢弃这个消息");
        }
        return BaseResult.success();
    }
}
